package com.yx.backstage.manager;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.backstage.config.global.CommonResult;
import com.yx.backstage.enums.NoticeType;
import com.yx.backstage.enums.Sex;
import com.yx.backstage.model.SysUser;
import com.yx.backstage.model.SystemNotice;
import com.yx.backstage.model.param.SystemNoticeParam;
import com.yx.backstage.model.vo.SysUserVo;
import com.yx.backstage.model.vo.SystemNoticeVo;
import com.yx.backstage.service.SysUserService;
import com.yx.backstage.service.SystemNoticeService;
import com.yx.backstage.util.LoginUtil;
import com.yx.backstage.util.TimeFormat;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author YangXiong
 */
@Component
public class SystemNoticeManager {
    @Resource
    private SystemNoticeService systemNoticeService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private DictManager dictManager;
    @Resource
    private LoginUtil loginUtil;

    public CommonResult<List<SystemNoticeVo>> getUserAdvise() {
        LambdaQueryWrapper<SystemNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemNotice::getType, NoticeType.TO_SYSTEM.getCode());
        wrapper.eq(SystemNotice::getOldNoticeId, 0);
        wrapper.orderByDesc(SystemNotice::getModifyTime);
        List<SystemNotice> list = systemNoticeService.list(wrapper);
        List<SystemNoticeVo> result = handlerRes(list);
        return CommonResult.success(result);
    }

    private List<SystemNoticeVo> handlerRes(List<SystemNotice> list) {
        List<SystemNoticeVo> vos = new ArrayList<>();
        list.forEach(one -> {
            SystemNoticeVo vo = new SystemNoticeVo();
            getVo(one, vo);
            LambdaQueryWrapper<SystemNotice> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SystemNotice::getOldNoticeId, one.getId());
            List<SystemNotice> noticeList = systemNoticeService.list(queryWrapper);
            List<SystemNoticeVo> os = new ArrayList<>();
            noticeList.forEach(o -> {
                SystemNoticeVo a = new SystemNoticeVo();
                getVo(o, a);
                os.add(a);
            });
            vo.setChild(os);
            vos.add(vo);
        });
        return vos;
    }

    private void getVo(SystemNotice one, SystemNoticeVo vo) {
        vo.setId(one.getId());
        vo.setContext(one.getContext());
        vo.setCreateTime(DateUtil.format(one.getCreateTime(), TimeFormat.TIME_FORMAT));
        vo.setModifyTime(DateUtil.format(one.getModifyTime(), TimeFormat.TIME_FORMAT));
        SysUser user = sysUserService.getById(one.getFromId());
        SysUserVo sysUserVo = sysUserToVo(user);
        vo.setUser(sysUserVo);
    }

    private SysUserVo sysUserToVo(SysUser one) {
        SysUserVo vo = new SysUserVo();
        if(Objects.nonNull(one)){
            vo.setId(one.getId());
            vo.setAvatar(one.getAvatar());
            vo.setAccount(one.getAccount());
            vo.setUserName(one.getUserName());
            vo.setBirthday(DateUtil.format(one.getBirthday(), TimeFormat.DATE_FORMAT));
            vo.setEmail(one.getEmail());
            vo.setPhone(one.getPhone());
            vo.setHobby(dictManager.getTextByValue(one.getHobby()));
            vo.setSex(Sex.getValueByKey(one.getSex()));
            vo.setEnable(one.getEnable());
            SysUser userServiceById = sysUserService.getById(one.getUpdateUser());
            vo.setOperatorName(userServiceById.getUserName());
            vo.setCreateTime(DateUtil.format(one.getCreateTime(), TimeFormat.TIME_FORMAT));
            vo.setModifyTime(DateUtil.format(one.getUpdateTime(), TimeFormat.TIME_FORMAT));
            vo.setLastLoginTime(DateUtil.format(one.getLastLoginTime(), TimeFormat.TIME_FORMAT));
        }
        return vo;
    }

    public CommonResult<String> save(SystemNoticeParam param) {
        SysUser currentUser = loginUtil.getCurrentUser();
        SystemNotice notice = new SystemNotice();
        notice.setContext(param.getContext());
        notice.setFromId(currentUser.getId());
        notice.setOperatorId(currentUser.getId());
        notice.setOperatorName(currentUser.getUserName());
        notice.setToId(param.getToId());
        notice.setOldNoticeId(param.getOldNoticeId());
        notice.setCreateTime(DateUtil.date());
        notice.setModifyTime(DateUtil.date());
        notice.setType(NoticeType.TO_SYSTEM.getCode());
        boolean save = systemNoticeService.save(notice);
        if (save) {
            return CommonResult.success("操作成功");
        }
        return CommonResult.fail("操作失败");
    }

    public CommonResult<List<SystemNoticeVo>> getMessage() {
        LambdaQueryWrapper<SystemNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemNotice::getType, NoticeType.ALL.getCode());
        wrapper.eq(SystemNotice::getOldNoticeId, 0);
        wrapper.eq(SystemNotice::getFromId, -1L);
        wrapper.eq(SystemNotice::getOperatorId, -9L);
        wrapper.orderByDesc(SystemNotice::getModifyTime);
        wrapper.between(SystemNotice::getCreateTime, DateUtil.yesterday().offset(DateField.DAY_OF_MONTH, -2), DateUtil.date());
        List<SystemNotice> list = systemNoticeService.list(wrapper);
        List<SystemNoticeVo> result = handlerRes(list);
        return CommonResult.success(result);
    }
}
